package edu.zime.iot.dao;

import edu.zime.iot.entity.Course;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class CourseDao
{
    @Autowired
    private JdbcTemplate jdbcTemplate;

    public List<Course> getAll()
    {
        return jdbcTemplate.query("select * from course",new BeanPropertyRowMapper<>(Course.class));
    }
    public int addCourse(Course course)
    {
        return jdbcTemplate.update("INSERT INTO course(lesson,teacher) VALUES(?,?)",course.getLesson(),course.getTeacher());
    }
    public int updateCourse(Course course)
    {
        return jdbcTemplate.update("UPDATE course SET lesson=?,teacher=? WHERE id=?",course.getLesson(),course.getTeacher(),course.getId());
    }
    public int deleteCourseById(int id)
    {
        return jdbcTemplate.update("DELETE FROM Course WHERE id=?",id);
    }
    public Course getCourseById(int id)
    {
        return jdbcTemplate.queryForObject("select * from where id=?", new BeanPropertyRowMapper<>(Course.class),id);
    }
}

